
#include "maths.h"

int isqrt(int value)
{
  int root = 0;

#define STEP(shift) \
    if((0x40000000 >> shift) + root <= value)          \
    {                                                   \
        value -= (0x40000000 >> shift) + root;          \
        root = (root >> 1) | (0x40000000 >> shift);     \
    }                                                   \
    else                                                \
    {                                                   \
        root >>= 1;                                     \
    }

  STEP( 0); STEP( 2); STEP( 4); STEP( 6);
  STEP( 8); STEP(10); STEP(12); STEP(14);
  STEP(16); STEP(18); STEP(20); STEP(22);
  STEP(24); STEP(26); STEP(28); STEP(30);

  // round to the nearest integer, cuts max error in half

  if (root < value) root++;

  return root;
}

const int g_fpRecip[RECIPROCAL_MAX]=
{
0,65536,32768,21845,16384,13107,10922,9362,8192,7281,6553,
5957,5461,5041,4681,4369,4096,3855,3640,3449,3276,
3120,2978,2849,2730,2621,2520,2427,2340,2259,2184,
2114,2048,1985,1927,1872,1820,1771,1724,1680,1638,
1598,1560,1524,1489,1456,1424,1394,1365,1337,1310,
1285,1260,1236,1213,1191,1170,1149,1129,1110,1092,
1074,1057,1040,1024,1008,992,978,963,949,936,
923,910,897,885,873,862,851,840,829,819,
809,799,789,780,771,762,753,744,736,728,
720,712,704,697,689,682,675,668,661,655,
648,642,636,630,624,618,612,606,601,595,
590,585,579,574,569,564,560,555,550,546,
541,537,532,528,524,520,516,512,508,504,
500,496,492,489,485,481,478,474,471,468,
464,461,458,455,451,448,445,442,439,436,
434,431,428,425,422,420,417,414,412,409,
407,404,402,399,397,394,392,390,387,385,
383,381,378,376,374,372,370,368,366,364,
362,360,358,356,354,352,350,348,346,344,
343,341,339,337,336,334,332,330,329,327,
326,324,322,321,319,318,316,315,313,312,
310,309,307,306,304,303,302,300,299,297,
296,295,293,292,291,289,288,287,286,284,
283,282,281,280,278,277,276,275,274,273,
271,270,269,268,267,266,265,264,263,262,
261,260,259,258,257 };

const int g_fpSin[361]=
{ 0,1143,2287,3429,4571,5711,6850,7986,9120,10252,11380,
12504,13625,14742,15854,16961,18064,19160,20251,21336,22414,
23485,24550,25606,26655,27696,28729,29752,30767,31772,32767,
33753,34728,35693,36647,37589,38521,39440,40347,41243,42125,
42995,43852,44695,45525,46340,47142,47929,48702,49460,50203,
50931,51643,52339,53019,53683,54331,54963,55577,56175,56755,
57319,57864,58392,58903,59395,59870,60326,60763,61183,61583,
61965,62328,62672,62997,63302,63589,63856,64103,64331,64540,
64729,64898,65047,65176,65286,65376,65446,65496,65526,65535,
65526,65496,65446,65376,65286,65176,65047,64898,64729,64540,
64331,64103,63856,63589,63302,62997,62672,62328,61965,61583,
61183,60763,60326,59870,59395,58903,58393,57864,57319,56755,
56175,55577,54963,54331,53684,53019,52339,51643,50931,50203,
49460,48702,47930,47142,46341,45525,44695,43852,42995,42125,
41243,40348,39440,38521,37590,36647,35693,34728,33753,32768,
31772,30767,29752,28729,27696,26656,25607,24550,23486,22414,
21336,20251,19161,18064,16962,15854,14742,13625,12505,11380,
10252,9121,7986,6850,5712,4571,3430,2287,1143,0,
-1143,-2286,-3429,-4571,-5711,-6850,-7986,-9120,-10251,-11380,
-12504,-13625,-14742,-15854,-16961,-18063,-19160,-20251,-21336,-22414,
-23485,-24550,-25606,-26655,-27696,-28728,-29752,-30767,-31772,-32767,
-33753,-34728,-35693,-36647,-37589,-38520,-39440,-40347,-41242,-42125,
-42995,-43851,-44695,-45524,-46340,-47142,-47929,-48702,-49460,-50203,
-50930,-51642,-52339,-53019,-53683,-54331,-54962,-55577,-56175,-56755,
-57318,-57864,-58392,-58903,-59395,-59870,-60326,-60763,-61183,-61583,
-61965,-62328,-62672,-62997,-63302,-63589,-63856,-64103,-64331,-64540,
-64729,-64898,-65047,-65176,-65286,-65376,-65446,-65496,-65526,-65535,
-65526,-65496,-65446,-65376,-65286,-65177,-65047,-64898,-64729,-64540,
-64331,-64103,-63856,-63589,-63302,-62997,-62672,-62328,-61965,-61583,
-61183,-60764,-60326,-59870,-59395,-58903,-58393,-57864,-57319,-56755,
-56175,-55577,-54963,-54331,-53684,-53019,-52339,-51643,-50931,-50203,
-49460,-48702,-47930,-47142,-46341,-45525,-44695,-43852,-42995,-42125,
-41243,-40348,-39440,-38521,-37590,-36647,-35693,-34729,-33753,-32768,
-31772,-30767,-29753,-28729,-27697,-26656,-25607,-24550,-23486,-22414,
-21336,-20252,-19161,-18064,-16962,-15854,-14742,-13626,-12505,-11380,
-10252,-9121,-7987,-6850,-5712,-4571,-3430,-2287,-1144,0 };

const int g_fpCos[361]=
{65536,65526,65496,65446,65376,65286,65176,65047,64898,64729,64540,
64331,64103,63856,63589,63302,62997,62672,62328,61965,61583,
61183,60763,60326,59870,59395,58903,58393,57864,57319,56755,
56175,55577,54963,54331,53683,53019,52339,51643,50931,50203,
49460,48702,47930,47142,46340,45525,44695,43852,42995,42125,
41243,40348,39440,38521,37589,36647,35693,34728,33753,32768,
31772,30767,29752,28729,27696,26655,25607,24550,23486,22414,
21336,20251,19160,18064,16962,15854,14742,13625,12504,11380,
10252,9120,7986,6850,5711,4571,3429,2287,1143,0,
-1143,-2287,-3429,-4571,-5711,-6850,-7986,-9120,-10251,-11380,
-12504,-13625,-14742,-15854,-16961,-18064,-19160,-20251,-21336,-22414,
-23485,-24550,-25606,-26655,-27696,-28728,-29752,-30767,-31772,-32767,
-33753,-34728,-35693,-36647,-37589,-38520,-39440,-40347,-41243,-42125,
-42995,-43852,-44695,-45525,-46340,-47142,-47929,-48702,-49460,-50203,
-50930,-51642,-52339,-53019,-53683,-54331,-54963,-55577,-56175,-56755,
-57319,-57864,-58392,-58903,-59395,-59870,-60326,-60763,-61183,-61583,
-61965,-62328,-62672,-62997,-63302,-63589,-63856,-64103,-64331,-64540,
-64729,-64898,-65047,-65176,-65286,-65376,-65446,-65496,-65526,-65535,
-65526,-65496,-65446,-65376,-65286,-65177,-65047,-64898,-64729,-64540,
-64331,-64103,-63856,-63589,-63302,-62997,-62672,-62328,-61965,-61583,
-61183,-60763,-60326,-59870,-59395,-58903,-58393,-57864,-57319,-56755,
-56175,-55577,-54963,-54331,-53684,-53019,-52339,-51643,-50931,-50203,
-49460,-48702,-47930,-47142,-46341,-45525,-44695,-43852,-42995,-42125,
-41243,-40348,-39440,-38521,-37590,-36647,-35693,-34728,-33753,-32768,
-31772,-30767,-29752,-28729,-27696,-26656,-25607,-24550,-23486,-22414,
-21336,-20251,-19161,-18064,-16962,-15854,-14742,-13625,-12505,-11380,
-10252,-9121,-7987,-6850,-5712,-4571,-3430,-2287,-1144,0,
1143,2286,3429,4571,5711,6850,7986,9120,10251,11379,
12504,13625,14742,15854,16961,18063,19160,20251,21336,22414,
23485,24549,25606,26655,27696,28728,29752,30767,31772,32767,
33753,34728,35693,36647,37589,38520,39440,40347,41242,42125,
42995,43851,44695,45524,46340,47142,47929,48702,49460,50203,
50930,51642,52339,53019,53683,54331,54962,55577,56175,56755,
57318,57864,58392,58903,59395,59869,60326,60763,61183,61583,
61965,62328,62672,62997,63302,63589,63856,64103,64331,64540,
64729,64898,65047,65176,65286,65376,65446,65496,65526,65535};
